var express = require('express');
var router = express.Router();
let path = require('path');
let fs = require('fs');

/* */
router.get('/login', function (req, res, next) {
  res.render('login', { title: 'Express' });
});

// POST
router.post('/login', (req, res, next) => {
  //获取前端传递过来的参数
  let { username, password } = req.body;

  // 判断输入框是否有内容
  if (!username || !password) {
    next('请输入有效内容');
    return;
  }

  //  获取persons.json里面的数据
  let data = JSON.parse(fs.readFileSync(path.join(__dirname, '../persons.json')));

  // 判断登入是否正确
  let s = data.find(item => username == item.username && password == item.password);
  if (s) {
    res.render('aa', { exactness: '正确' });
    return;
  }
  next('账号或密码不一致,请重新输入!');

})

module.exports = router;
